import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/chart/kq_chart.dart';
import 'package:kq_flutter_core_widget/widgets/chart/pie/pie_chart.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';

/// 饼状图
class PieChart3DPage extends StatelessWidget {
  const PieChart3DPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: '3D饼状图',
        showBack: false,
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart2(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart3(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: Text(
                "下面为两种无数据的展示方式：",
                style: TextStyle(fontSize: 20.sp),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar2(),
            ),
          )
        ],
      ),
    );
  }

  /// 常规饼状图
  Widget buildPieChart() {
    return SizedBox(
      width: 260.r,
      height: 260.r,
      child: KqChart(
        delegate: KqPieChart3DDelegate(
          animDuration: const Duration(seconds: 2),
          radius: 90.r,
          height: 30.r,
          dataRender: PieChart3DDataRender(highLightRender: PieChart3DHighLightRender(
              offsetRadius: 0,
              colorBuilder: (p0) {
                return HSLColor.fromColor(p0.color)
                    .withLightness(0.7)
                    .toColor();
              })),
          labelRender: PieChart3DLabelRender(),
          gestureHandler: PieChart3DGestureHandler(),
          data: [
            PieChartEntity(
                color: Colors.redAccent,
                value: 10,
                drawLabel: true,
                label: "测试",
                labelSize: 10.sp,
                labelColor: Colors.white,
                secondLabel: "测试2",
                secondLabelSize: 8.sp),
            PieChartEntity(
                color: Colors.blueAccent,
                value: 33.5,
                drawLabel: true,
                label: "测试1",
                labelSize: 10.sp,
                labelColor: Colors.white,
                secondLabel: "测试12",
                secondLabelSize: 8.sp),
            PieChartEntity(
                color: Colors.brown,
                value: 10,
                drawLabel: true,
                label: "测试2",
                labelSize: 10.sp,
                labelColor: Colors.white,
                secondLabel: "测试22",
                secondLabelSize: 8.sp),
            PieChartEntity(
                color: Colors.lightGreen,
                value: 19.3,
                drawLabel: true,
                label: "测试3",
                labelSize: 10.sp,
                labelColor: Colors.white,
                secondLabel: "测试32",
                secondLabelSize: 8.sp)
          ],
          labelPosition: LabelPosition.inside(),
          sliceSpace: 0.r,
          innerRadius: 0.r,
        ),
      ),
    );
  }

  /// 圆环饼状图
  Widget buildPieChart2() {
    return SizedBox(
      width: 260.r,
      height: 260.r,
      child: KqChart(
        delegate: KqPieChart3DDelegate.withDefault(
          animDuration: const Duration(seconds: 2),
          radius: 90.r,
          height: 30.r,
          data: [
            PieChartEntity(
                color: Colors.redAccent,
                value: 10,
                drawLabel: true,
                label: "测试",
                labelSize: 10.sp,
                labelColor: Colors.redAccent,
                secondLabel: "测试2",
                secondLabelSize: 8.sp),
            PieChartEntity(
                color: Colors.blueAccent,
                value: 33.5,
                drawLabel: true,
                label: "测试1",
                labelSize: 10.sp,
                labelColor: Colors.blueAccent,
                secondLabel: "测试12",
                secondLabelSize: 8.sp),
            PieChartEntity(
                color: Colors.brown,
                value: 10,
                drawLabel: true,
                label: "测试2",
                labelSize: 10.sp,
                labelColor: Colors.brown,
                secondLabel: "测试22",
                secondLabelSize: 8.sp),
            PieChartEntity(
                color: Colors.lightGreen,
                value: 19.3,
                drawLabel: true,
                label: "测试3",
                labelSize: 10.sp,
                labelColor: Colors.lightGreen,
                secondLabel: "测试32",
                secondLabelSize: 8.sp)
          ],
          labelPosition: LabelPosition.outside(
            lineFactory: (entity) {
              return IndicatorLine(
                  color: entity.color,
                  drawStartCircle: true,
                  circleColor: entity.color,
                  offset: 4.r);
            },
          ),
          sliceSpace: 4.r,
          innerRadius: 60.r,
        ),
      ),
    );
  }

  /// 丁达尔玫瑰图
  Widget buildPieChart3() {
    return SizedBox(
      width: 260.r,
      height: 260.r,
      child: KqChart(
        delegate: KqPieChart3DDelegate(
          animDuration: const Duration(seconds: 2),
          radius: 90.r,
          height: 30.r,
          dataRender: PieChart3DDataRender(highLightRender: PieChart3DHighLightRender(addedRadius: 20.r, offsetRadius: 0)),
          gestureHandler: PieChart3DGestureHandler(),
          data: [
            PieChartEntity(color: Colors.redAccent, value: 10),
            PieChartEntity(color: Colors.brown, value: 15, addedRadius: 10.r),
            PieChartEntity(
                color: Colors.lightGreen, value: 20, addedRadius: 20.r),
            PieChartEntity(
                color: Colors.blueAccent, value: 25, addedRadius: 30.r),
          ],
          sliceSpace: 0.r,
          innerRadius: 60.r,
        ),
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar() {
    return Container(
      width: 160.r,
      height: 160.r,
      color: Colors.lightBlue[200],
      child: KqChart(
        delegate: KqPieChart3DDelegate.withDefault(radius: 70.r),
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar2() {
    return SizedBox(
      width: 260.r,
      height: 260.r,
      child: KqChart(
        delegate: KqPieChart3DDelegate(
          radius: 90.r,
          innerRadius: 60.r,
          baseMapColor: Colors.grey,
          height: 30.r,
          dataRender: PieChart3DDataRender(),
          gestureHandler: PieChart3DGestureHandler(),
        ),
      ),
    );
  }
}
